<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" :title="title" :width="width" :visible="visible" @ok="handleOk" @cancel="handleCancel" @close="handleClose"  destroyOnClose>
    <DefectManageForm ref="registerForm" @ok="submitCallback" :formDisabled="disableSubmit" :defectRecords="defectRecords" :formBpm="false" :continueFlag="continueFlag"></DefectManageForm>
    <template #insertFooter>
      <a-button @click="continueAdd">继续添加</a-button>
    </template>
  </BasicDrawer>
</template>

<script lang="ts" setup>
import {computed, defineComponent, defineExpose, nextTick, ref, unref} from 'vue';
import {BasicDrawer, useDrawerInner} from '/@/components/Drawer';
import DefectManageForm from './DefectManageForm.vue'

const emit = defineEmits(['register', 'success']);
const title = ref<string>('');
const width = ref<number>(1300);
const visible = ref<boolean>(false);
const disableSubmit = ref<boolean>(false);
const registerForm = ref();
const isUpdate = ref(true);
const continueFlag = ref(true);
// const shown = ref<boolean>(false);
let defectRecords = [];
const formData = ref({
    id: '',
    projectId: '',   
    batchId: '',   
    systemId: '',   
    systemModuleId: '',   
    functionName: '',   
    defectLevel: '',   
    defectType: '',   
    defectTitle: '',   
    description: '',   
    testStage: '',   
    envType: '',   
    defectPriority: '',   
    bugStatus: '1',   
    solverId: '',   
    isInfects: '',   
    infects: '',   
    noSloveReason: '',   
    defectReason: '',   
    defectReasonCata: '',   
    defectImg: '',   
    defectId: '',   
    commets: '',   
  });
const created = () => {

};
created();

const [registerDrawer,{ closeDrawer }] = useDrawerInner((data) => {
  // 方式1
  console.log('useDrawerInner:'+data.isUpdate)
  isUpdate.value = !!data?.isUpdate;
  disableSubmit.value = data.disableSubmit;
  registerForm.value.validatorRules.defectReasonCata[0].required = data.shown;
  registerForm.value.validatorRules.isInfects[0].required = data.shown;
  registerForm.value.validatorRules.infects[0].required = data.shown;
  registerForm.value.validatorRules.noSloveReason[0].required = data.shown;
  registerForm.value.validatorRules.defectReason[0].required = data.shown;
  if (unref(isUpdate)) {
    // console.log('--------result--------');
    width.value = 1300;
    console.log('result:' + data.records);
    registerForm.value.validatorRules.noSloveReason[0].required = false;
    edit(data.record);
    defectRecords = data.records;
  } else{
    width.value = 800;
    defectRecords = []
    add();
  }
});
/**
 * 继续
 */
function continueAdd() {
  // console.log('continueAdd:' + JSON.stringify(registerForm.value.formData))
  // formData.value.projectId = registerForm.value.formData.projectId
  // formData.value.batchId= registerForm.value.formData.batchId    
  // formData.value.systemId= registerForm.value.formData.systemId 
  // formData.value.systemModuleId= registerForm.value.formData.systemModuleId   
  // formData.value.functionName= registerForm.value.formData.functionName   
  // formData.value.defectLevel= registerForm.value.formData.defectLevel    
  // formData.value.defectType= registerForm.value.formData.defectType  
  // formData.value.defectTitle= registerForm.value.formData.defectTitle   
  // formData.value.testStage= registerForm.value.formData.testStage  
  // formData.value.envType= registerForm.value.formData.envType    
  // formData.value.defectPriority= registerForm.value.formData.defectPriority   
  // formData.value.bugStatus= registerForm.value.formData.bugStatus 
  registerForm.value.continueSubmitForm();
}
/**
 * 新增
 */
function add() {
  title.value = '提交缺陷';
  visible.value = true;
  nextTick(() => {
    registerForm.value.shown = false;
    registerForm.value.formData.bugStatus='1'
    registerForm.value.add();
  });
}

/**
 * 编辑
 * @param record
 */
function edit(record) {
  title.value = disableSubmit.value ? '缺陷详情' : '编辑缺陷';
  visible.value = true;
  nextTick(() => {
    registerForm.value.shown = true;
    registerForm.value.edit(record);
  });
}

/**
 * 确定按钮点击事件
 */
function handleOk() {
  registerForm.value.submitForm();
}

/**
 * form保存回调事件
 */
function submitCallback() {
  handleCancel();
  emit('success');
}

/**
 * 取消按钮回调事件
 */
function handleCancel() {
  visible.value = false;
}

defineExpose({
  add,
  edit,
  disableSubmit,
});
</script>

<style>
/**隐藏样式-modal确定按钮 */
.jee-hidden {
  display: none !important;
}
</style>
